﻿using CNav.Business.Validation;
using CNav.Common;
using CNav.DataEntity;
using CNav.Domain;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CNav.Business.Services
{
    public interface IComplaintService
    {
        ResponseData<bool> AddComplaintType(ComplaintType complaintType);
        ResponseData<ComplaintType> UpdateComplaintType(ComplaintType complaintType);
        ResponseData<bool> DeleteComplaintType(int id);
        ResponseData<List<ComplaintType>> GetComplaintTypes();
        ResponseData<ComplaintType> GetComplaintType(int id);

     }

    public class ComplaintService : ServiceBase, IComplaintService
    {
        public ResponseData<bool> AddComplaintType(ComplaintType complaintType)
        {
            var response = new ResponseData<bool>();

            var validator = new ComplaintTypeValidation();
            var result = validator.Validate(complaintType);
            if (result.IsValid)
            {
                if (ComplaintTypeExist(complaintType.Code))
                {
                    response.Error = new[] { "Duplicate complaint type." };
                    return response;
                }

                var complaintTypeDto = new ComplaintTypeDto();
                complaintTypeDto.Code = complaintType.Code.ToUpper();
                complaintTypeDto.Description = complaintType.Description;

                Context.Repository<ComplaintTypeDto>().Add(complaintTypeDto);
                Context.Commit();

                response.Data = true;
                return response;
            }
            response.Error = result.ErrorList();
            return response;
        }

        private bool ComplaintTypeExist(string code)
        {
            var uCode = code.ToUpper();
            ComplaintTypeDto complaintType = Context.Get<ComplaintTypeDto>(b => b.Code == uCode).ToList()
                   .FirstOrDefault();

            return complaintType != null;
        }

        public ResponseData<ComplaintType> UpdateComplaintType(ComplaintType complaintType)
        {
            var response = new ResponseData<ComplaintType>();

            var validator = new ComplaintTypeValidation();
            var result = validator.Validate(complaintType);
            if (result.IsValid)
            {
                if (ComplaintTypeExist(complaintType.Code))
                {
                    var complaintTypeDto = Context.Get<ComplaintTypeDto>(c => c.ID == complaintType.ID).ToList().FirstOrDefault();
                    complaintTypeDto.Description = complaintType.Description;

                    Context.Repository<ComplaintTypeDto>().Update(complaintTypeDto);
                    Context.Commit();
                    response = GetComplaintType(complaintType.ID);

                    return response;
                }
                response.Error = new[] { "Complaint provided does not exist." };
                return response;
            }
            response.Error = result.ErrorList();
            return response;
        }

        public ResponseData<bool> DeleteComplaintType(int id)
        {
            var response = new ResponseData<bool>();

            var complaintTypeDto = Context.Get<ComplaintTypeDto>(c => c.ID == id).ToList().FirstOrDefault();
            if (complaintTypeDto != null)
            {
                Context.Repository<ComplaintTypeDto>().Delete(complaintTypeDto);
                Context.Commit();

                response.Data = true;
                return response;
            }
            response.Error = new[] { "Complaint provided does not exist." };
            return response;
        }

        public ResponseData<List<ComplaintType>> GetComplaintTypes()
        {
            var response = new ResponseData<List<ComplaintType>>();
            var complaintTypeDtos = Context.Get<ComplaintTypeDto>().ToList();

            response.Data = complaintTypeDtos.Select(c => new ComplaintType { ID = c.ID, Code = c.Code, Description = c.Description }).ToList();
            return response;
        }

        public ResponseData<ComplaintType> GetComplaintType(int id)
        {
            var response = new ResponseData<ComplaintType>();
            var complaintTypeDto = Context.Get<ComplaintTypeDto>(c => c.ID == id).ToList().FirstOrDefault();
            if (complaintTypeDto != null)
            {
                var complaintType = new ComplaintType();

                complaintType.ID = complaintTypeDto.ID;
                complaintType.Code =complaintTypeDto.Code;
                complaintType.Description = complaintTypeDto.Description;

                response.Data = complaintType;
                return response;
            }
            response.Error = new[] { "Complaint id does not exist." };
            return response;
        }
    }
}